<?php
namespace app\api\controller;
use app\api\controller\BaseController;
use app\api\model\User;
use think\facade\Db;
use lizhili\Time;

class My extends BaseController
{
    public function ajax()
    {
        $data=input('post.');
		if(!$data){
            return json(['code'=>400,'message'=>'提交数据错误！']);
		}
        $lizhili=BaseController::lizhili($data,true);
       if($lizhili['code']==0){
           return json(['code'=>10005,'message'=>'提交数据错误！']);
       }
        if($lizhili['code']===1){
            return json(['code'=>10001,'message'=>'提交超时，请重复提交！']);
        }
        if($lizhili['code']===2){
            return json(['code'=>10002,'message'=>'重复提交，请稍后重试！']);
        }
		if($lizhili['code']===3){
			return json(['code'=>10000,'message'=>'token错误']);
		}
		$user_model=new User();
        $user=$user_model->where('token',$data['token'])->find();
        //生成海报
        if ($data['type']=='get_my_info_haibao') {
            if($user=$user_model->where('token',$data['token'])->find()){
                if(!$user['haibao']){
                    $host=request()->header('host');
                    $filename='haibao/erweima/'.$user['id'].'.png';
                    $haibao=\erweima\Qrcode::draw('http://'.$host.'/xia/main?f_user_no='.$user['user_no'], $filename);
                    $user_model->where('id',$user['id'])->update(['erweima'=>$haibao]);
                }
                return json(['code'=>200,'message'=>'成功','data'=>$user]);
            }else{
                return json(['code'=>400,'message'=>'失败']);
            }
        }
        //查看下一级
        if($data['type']=='get_my_tui_info'){
            $user=$user_model->where('token',$data['token'])->find();
            $shang=$user_model->where('id',$user['fid'])->find();
            if(!$shang){
                $shang=[
                    'phone'=>'',
                    'user_no'=>'',
                ];
            }
            $list=$user_model->where('fid',$user['id'])->select();
            return json(['code'=>200,'message'=>'成功','data'=>$list,'shang'=>$shang]);
        }
        //修改个人中心
        if ($data['type']=='update_my_info') {
            $info=json_decode($data['info'],true);
            if($user=$user_model->where('token',$data['token'])->find()){
                $user_model->where('id',$user['id'])->update([
                    'name'=>$info['name'],
                    'gender'=>$data['gender'],
                    'address'=>$info['address'],
                    'sheng'=>$data['sheng'],
                    'shi'=>$data['shi'],
                    'xian'=>$data['xian'],
                    'sheng_html'=>$data['sheng_html'],
                ]);
                return json(['code'=>200,'message'=>'成功']);
            }else{
                return json(['code'=>400,'message'=>'失败']);
            }
        }
        //查看是否有权限
        if ($data['type']=='get_my_info') {
            if($user){
                if($user['fid']){
                    $user['shang']=$user_model->where('id',$user['fid'])->value('user_no');
                }
                $mall_config=Db::name('mall_config')->where('id','in',[8,9,13,18])->column('value','key');
                return json(['code'=>200,'message'=>'成功','data'=>$user,'mall_config'=>$mall_config]);
            }else{
                return json(['code'=>400,'message'=>'失败']);
            }
        }
        return json(['code'=>400,'message'=>'非法获取']);
    }
}
